package com.ukao.steward.ui.logistics.LeverFactory;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseActivity;
import com.ukao.steward.bean.ScanCodeBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.dialog.BatchDialogFragment;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.widget.StateButton;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;

import static com.ukao.steward.R.id.sure_btn;

/**
 * 确认批次
 */
public class SureBatchActivity extends BaseActivity {


    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.error)
    TextView error;
    @BindView(R.id.error_txt)
    TextView errorTxt;
    @BindView(R.id.backs_btn)
    StateButton backsBtn;
    @BindView(R.id.error_layout)
    LinearLayout errorLayout;
    @BindView(R.id.code)
    TextView code;
    @BindView(R.id.clothCnt)
    TextView clothCnt;
    @BindView(R.id.annexCnt)
    TextView annexCnt;
    @BindView(R.id.storeName)
    TextView storeName;
    @BindView(R.id.sure_btn)
    StateButton sureBtn;
    @BindView(R.id.cancel_btn)
    StateButton cancelBtn;
    @BindView(R.id.receive_layout)
    LinearLayout receiveLayout;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ScanCodeBean mScanCodeBean;
    private int type;
    public int typeCode;
    private String codeMsg;
    private Handler mHandler=new Handler();
    private String hite;
    public boolean isReceiveSucceed;
    private BatchDialogFragment mBatchFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_sure_batch);
        ButterKnife.bind(this);
        mScanCodeBean = getIntent().getParcelableExtra(Constant.ARG_PARAM1);
        type = getIntent().getIntExtra(Constant.ARG_PARAM2, 0);
        initView();
    }

    @Override
    protected void initView() {
        switch (type) {
            case 1:  /*入厂*/
                title.setText("确定袋号");
                codeMsg = "袋号：";
                hite="来自 ";
                break;
            case 2:/*出厂批次*/
                codeMsg = "批号：";
                title.setText("确定批次");
                hite="送至 ";
                break;
            case 3:/*入厂订单*/
                codeMsg = "订单：";
                title.setText("确定订单");
                hite="来自 ";
                break;

            case 4:/*出厂订单*/
                codeMsg = "订单：";
                title.setText("确定订单");
                hite="送至 ";
                break;


        }

        if (!CheckUtils.isNull(mScanCodeBean)) {
            String  orderNo=mScanCodeBean.getOrderNo();
            if (!CheckUtils.isEmpty(orderNo)){/*确定接收订单*/
                int  orderCount=mScanCodeBean.getProCnt() + mScanCodeBean.getAnnexCnt();
                clothCnt.setText( orderCount+ "");
                code.setText(codeMsg+ orderNo);
                storeName.setText(hite+ mScanCodeBean.getStoreName());
                annexCnt.setText("衣物 " + mScanCodeBean.getProCnt() + " 件" + ",附件 " + mScanCodeBean.getAnnexCnt() + " 件");
            }else {
                int  batchCount= mScanCodeBean.getClothCnt() + mScanCodeBean.getAnnexCnt();
                clothCnt.setText(batchCount + "");
                code.setText(codeMsg+ mScanCodeBean.getCode());
                storeName.setText(hite+ mScanCodeBean.getStoreName());
                annexCnt.setText("衣物 " + mScanCodeBean.getClothCnt() + " 件" + ",附件 " + mScanCodeBean.getAnnexCnt() + " 件");
            }


        }

    }


    @OnClick({sure_btn, R.id.cancel_btn, R.id.back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case sure_btn:
                if (!CheckUtils.isNull(mScanCodeBean))
                    switch (type) {
                        case 1:
                            receive(mScanCodeBean.getBatchInId()+"");
                            break;
                        case 2:
                            receive(mScanCodeBean.getBatchOutId()+"");
                            break;
                        case 3:
                            receive(mScanCodeBean.getOrderNo()+"");
                            break;
                        case 4:
                            receive(mScanCodeBean.getOrderNo());
                            break;
                    }

                break;


            case R.id.cancel_btn:
                finish();
                break;
            case R.id.back_btn:
                finish();
                break;
        }
    }

    /**
     * 入厂  接收
     */
    public void receive(String batchInId) {
        sureBtn.setText("正在接收....");
        Observable observable = null;
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        switch (type) {
            case 1:
                mHashMap.put("batchInId", batchInId );
                observable = apiStores().receive(Constant.createParameter(mHashMap));

                break;
            case 2:
                mHashMap.put("batchOutId", batchInId );
                observable = apiStores().batchOutReceive(Constant.createParameter(mHashMap));
                break;
            case  3 :
                mHashMap.put("orderNo",batchInId );
                observable = apiStores().sureFactoryIn(Constant.createParameter(mHashMap));

                break;
            case 4:
                mHashMap.put("orderNo",batchInId);
                observable = apiStores().orderfactoryOutReceive(Constant.createParameter(mHashMap));

                break;

        }

        addSubscription(observable,
                new ApiCallback<StringBean>() {



                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            isReceiveSucceed =true;

                        } else if (model.getHttpCode() == 400) {
                            isReceiveSucceed =false;
                            title.setText("错误");
                            errorTxt.setText(model.getMsg());
                        }
                        sureBtn.setText("确定接收");
                        showHiteDialog(model.getMsg());


                    }

                    @Override
                    public void onFinish() {


                    }
                });
    }

    private void showHiteDialog(String  msg) {
        mBatchFragment=BatchDialogFragment.createBuilder(getSupportFragmentManager())
                .errorMsg(msg)
                .isReceiveSucceed(isReceiveSucceed)
                .show();
        mHandler.postDelayed(getRunnable(), 1200);

    }

    @NonNull
    private Runnable getRunnable() {
        return () -> {
            if (mBatchFragment!=null&&mBatchFragment.getDialog()!=null&&mBatchFragment.getDialog().isShowing()){
                mBatchFragment.dismiss();
            }
            if (isReceiveSucceed){
                    finish();
            }else{
                receiveLayout.setVisibility(View.GONE);
                errorLayout.setVisibility(View.VISIBLE);
            }

        };
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(getRunnable());
    }
}
